/**
* all depends on jquery.eventsignup.js
*/
$(document).ready(function(){	
	var disclaimerValidator = $('#eventSignupDisclaimerForm').validate({
			rules: {
			  recaptcha_response_field: {
				required: true,
			  },
			},});
			
	$('#eventSignupModify_submit').click(function(e){
		if( $("#eventSignupForm").valid() == false ){
			validator.focusInvalid();
			return;
		}
		$('.signup_form_errormsg').hide();
		
		// auth state : waiting
		ajax_waiting();
		
		data = $('#eventSignupForm').serialize();
		Dajaxice.events.eventSignupModifyJSON(modifyResultCallback, {'form':data}, {'error_callback': ajax_error});
		
		e.preventDefault();
		return false;
	});
	
	$('#eventSignupModify_delete').click(function(e){
		if( $("#eventSignupDisclaimerForm").valid() == false ){
			disclaimerValidator.focusInvalid();
			return;
		}
		disclaimer_ajax_waiting();
		data = $('#eventSignupDisclaimerForm').serialize();
		Dajaxice.events.eventSignupDisclaimerJSON(modifyResultCallback, {'form':data}, {'error_callback': disclaimer_ajax_error});
		
		e.preventDefault();
		return false;
	});
	
	function modifyResultCallback(data) {
		console.log("dajax callback " + data.result);
		 
		 hideInvalidSignupKey();
		 $('.modifySucceed').hide();
		 
		// auth state : finish
		disclaimer_ajax_finish();
		
		if( data.result == "captch_failed" )
		{
			$('.signup_disclaimer_errormsg_captcha').show();
		}
		// 1. event_id_not_exist
		else if( data.result == "event_id_not_exist")
		{
			$('.signup_disclaimer_errormsg_eventid').show();
		}
		// 2. participant_not_existed
		else if( data.result == "invalid_signupkey")
		{
			showInvalidSignupKey();
		}
		// 3. success signup
		else if( data.result == "succeed" )
		{
			window.location = data.redirect_to;
		}
	}
	function showInvalidSignupKey() { $('.signup_disclaimer_errormsg_invalidSignupKey').show(); }
	function hideInvalidSignupKey() { $('.signup_disclaimer_errormsg_invalidSignupKey').hide(); }
	function disclaimer_ajax_waiting()
	{
		$('.disclaimer-footer .controls').hide();
		$('.disclaimer-footer .progress').show();
	}
	function disclaimer_ajax_init()
	{
		$('.signup_disclaimer_errormsg_eventid').hide();
		$('.signup_disclaimer_errormsg_captcha').hide();
		$('.signup_disclaimer_errormsg_parti').hide();
		hideInvalidSignupKey();
		
		$('#signup_disclaimer_errormsg').hide();
		$('.disclaimer-footer .controls').show();
		$('.disclaimer-footer .progress').hide();
	}
	function disclaimer_ajax_error() { disclaimer_ajax_init(); }
	function disclaimer_ajax_finish() { disclaimer_ajax_init();}
});